package com.zjy.controller;

import com.zjy.bean.GoodsBrand;
import com.zjy.dto.ApiResult;
import com.zjy.service.GoodsBrandService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/4/11.
 */
@CrossOrigin
@Controller
@RequestMapping(value = "/goodsBrand")
public class GoodsBrandController {
    @Resource
    private GoodsBrandService goodsBrandService;

    /**
     * 增加商品品牌
     * @param goodsBrand
     * @return
     */
    @RequestMapping(value = "/addGoodsBrand",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addGoodsBrand(@RequestBody GoodsBrand goodsBrand){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsBrand) || ObjectUtils.isNullOrEmpty(goodsBrand.getType())){
            result.error(CLIENT_ERROR,"缺少参数：type");
            return result;
        }else{
            //判断传入的type是否已存在，若已存在则提示换一个type值
            int flag = goodsBrandService.checkExistType(goodsBrand.getType());
            if(flag == 1){
                result.error(CLIENT_ERROR,"type值已存在");
                return result;
            }else{
                try{
                    goodsBrandService.addGoodsBrand(goodsBrand);
                    result.setMsg("商品品牌插入成功！");
                }catch (Exception e){
                    result.error(SERVER_ERROR,e.getMessage());
                    return result;
                }
            }
        }
        return result;
    }

    /**
     * 根据Id软删除商品品牌
     * @param goodsBrand
     * @return
     */
    @RequestMapping(value = "/deleteGoodsBrandById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteGoodsBrandById(@RequestBody GoodsBrand goodsBrand){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsBrand) || ObjectUtils.isNullOrEmpty(goodsBrand.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String categoryId = goodsBrand.getId();
                int count = goodsBrandService.deleteGoodsBrandById(categoryId);
                if(count == 1){
                    result.setMsg("商品品牌删除成功！");
                }else if(count == 0){
                    result.setMsg("找不到此id对应的品牌，删除失败！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id更新商品品牌
     * @param goodsBrand
     * @return
     */
    @RequestMapping(value = "/updateGoodsBrandById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateGoodsBrandById(@RequestBody GoodsBrand goodsBrand){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsBrand) || ObjectUtils.isNullOrEmpty(goodsBrand.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                int count = goodsBrandService.updateGoodsBrandById(goodsBrand);
                if(count == 0){
                    result.error(SERVER_ERROR,"商品品牌更新失败！");
                    return result;
                }else if(count == 1){
                    result.setMsg("商品品牌更新成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id查找对应的商品品牌
     * @param goodsBrand
     * @return
     */
    @RequestMapping(value = "/selectGoodsBrandById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectGoodsBrandById(@RequestBody GoodsBrand goodsBrand){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsBrand) || ObjectUtils.isNullOrEmpty(goodsBrand.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String id = goodsBrand.getId();
                GoodsBrand goodsBrand1 = goodsBrandService.selectGoodsBrandById(id);
                if(ObjectUtils.isNullOrEmpty(goodsBrand1)){
                    result.setMsg("找不到此Id对应的商品品牌");
                }else {
                    result.setData(goodsBrand1);
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 查找所有的商品品牌
     * @return
     */
    @RequestMapping(value = "/selectAllGoodsBrand",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectAllGoodsBrand(){
        ApiResult result = new ApiResult();
        try{
            List<GoodsBrand> list = goodsBrandService.selectAllGoodsBrand();
            if(ObjectUtils.isNullOrEmpty(list)){
                result.setMsg("找不到商品品牌！");
            }else {
                result.setData(list);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }
}
